package cn.com.lsjz.nursemanager.controller.system;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import cn.com.lsjz.commons.result.ResultModel;
import cn.com.lsjz.nursemanager.model.system.SysUser;
import cn.com.lsjz.nursemanager.service.system.SysUserService;


/**
 * 用户请求头部分
 * @author liucan
 *
 */
@RestController
@RequestMapping("/admin/user")
public class SysUserController {
	
	@Autowired
	private SysUserService sysUserService;
	
	@RequestMapping(method=RequestMethod.POST)
	public ResultModel saveUser(SysUser user) {
		sysUserService.saveUser(user);
		return new ResultModel(true, "新增成功");
	}
	
	@RequestMapping(method=RequestMethod.PUT)
	public ResultModel editUser(SysUser user) {
		sysUserService.editUser(user);
		return new ResultModel(true, "更新成功");
	}
	
	@RequestMapping(method=RequestMethod.DELETE,value="/{id}")
	public ResultModel delUser(@PathVariable(value="id") Integer id) {
		sysUserService.delUser(id);
		return new ResultModel(true, "删除成功");
	}
	
	@RequestMapping(value="/{id}",method=RequestMethod.GET)
	public ResultModel getUser(@PathVariable(value="id") Integer id) {
		SysUser user = sysUserService.getUser(id);
		return new ResultModel(true, user, "获取单用户成功");
	}
	
	/**
	 * 根据不同的条件获取用户信息
	 * @param deptCode
	 * @param userName
	 * @return
	 */
	@RequestMapping(value="loadUserList",method=RequestMethod.POST)
	public ResultModel loadUserList(String deptCode,String userName) {
		List<SysUser> users = sysUserService.loadUserList(deptCode, userName);
		return new ResultModel(true, users, "查询成功");
	}
	
	@RequestMapping(value="/permission/{userId}",method=RequestMethod.GET)
	public ResultModel getPermission(@PathVariable(value="userId") String userId) {
		Map<String, Object> map = sysUserService.getPermission(userId);
		return new ResultModel(true, map, "获取用户角色");
	}
	
	/**
	 * 保存用户权限
	 * @return
	 */
	@RequestMapping(value="/permission",method=RequestMethod.POST)
	public ResultModel savePermission(String userId,String roles,String depts) {
		sysUserService.savePermission(userId, roles, depts);
		return new ResultModel(true, "用户权限保存成功");
	}
	
	
	@RequestMapping(value="/active",method=RequestMethod.POST)
	public ResultModel activeUser(Integer id,Integer active) {
		sysUserService.activeUser(id, active);
		return new ResultModel(true, active == 1 ? "启用成功" : "停用成功");
	}
	
	@RequestMapping(value="/modifyPwd",method=RequestMethod.POST)
	public ResultModel modifyPwd(Integer id,String oldPassword,String newPassword) {
		boolean flag = sysUserService.modifyPwd(id,oldPassword,newPassword);
		if(flag) {
			return new ResultModel(true,"修改成功");
		}else {
			return new ResultModel(false,"原始密码错误");
		}
	}
}
